package com.itheima.controller;

import com.itheima.mapper.StuMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/students")
public class StuController {
    @Autowired
    private StuService stuService;

    @GetMapping
    public Result getAllStudents(
            @RequestParam(required = false) Integer clazzId,
            @RequestParam(required = false) Integer degree,
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ){
        PageBean list = stuService.queryList(clazzId,degree,name,page,pageSize);
        return Result.success(list);
    }

    @DeleteMapping("/{ids}")
    public Result deleteStudentsById(@PathVariable List<Integer> ids){
        stuService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Student student){
        stuService.save(student);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getStudentById(@PathVariable Integer id){
//        log.info("id:=================================={}",id);
        Student student = stuService.getById(id);
        return Result.success(student);
    }

    @PutMapping
    public Result update(@RequestBody Student student){
        stuService.updateStudent(student);
        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    public Result redScore(@PathVariable Integer id, @PathVariable Integer score){
        stuService.updateScore(id,score);
        return Result.success();
    }




}
